10. Strategy

Strategy

ND079 JPND C2 L03 A11 Strategy

What is the Strategy Pattern?

  • You define an interface to represent a kind of task or problem.
  • Each concrete implementation defines a different "strategy" for solving the task.
  • The strategies can be swapped for each other because callers code against the interface.

Which statements are true about the strategy pattern?

SOLUTION:
  • Client code is agnostic of which concrete implementation is being used.
  • The strategies can be swapped at any time.
  • An interface defines a task. There may be many concrete implementations.

When can you use lambdas to implement concrete strategies?

SOLUTION: When the task is a functional interface.

What is the role of polymorphism in the strategy pattern?

SOLUTION: Polymorphism determines which concrete method implementation gets called when invoking the task interface.